package com.yilong.springcloudalibabanacos.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yilong.springcloudalibabanacos.sys.entity.SysDept;
import com.yilong.springcloudalibabanacos.sys.service.ISysDeptService;
import com.yilong.springcloudalibabanacos.sys.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;
import java.util.stream.LongStream;

/**
 * <p>
 * 部门管理 前端控制器
 * </p>
 *
 * @author SOS
 * @Api 表示对类的说明常用参数    类上面
 * @ApiOperation 说明方法的用途、作用	方法上面
 * @ApiModel 表示一个返回响应数据的信息    响应类
 * @ApiModelProperty 描述响应类的属性    属性
 * @ApiIgnore 忽略某个字段使之不显示在文档中    属性
 * @since 2021-08-15
 */
@Slf4j
@RestController
@RequestMapping("/sys/sys-dept")
//    @Api：用在请求的类上，表示对类的说明常用参数
//    参数	描述
//    tags	说明该类的作用，非空时将覆盖value的值
//    value	描述类的作用
//    description	对api资源的描述，在1.5版本后不再支持
//    basePath	基本路径可以不配置，在1.5版本后不再支持
//    position	如果配置多个Api 想改变显示的顺序位置，在1.5版本后不再支持
//    produces	设置MIME类型列表（output），例：“application/json, application/xml”，默认为空
//    authorizations	获取授权列表（安全声明），如果未设置，则返回一个空的授权值
//    hidden	默认为false， 配置为true 将在文档中隐藏
@Api(tags = "员工部门请求控制层")
public class SysDeptController {

    private final ISysDeptService iSysDeptService;

    private final RedisUtil redisUtil;

    public SysDeptController(ISysDeptService iSysDeptService, RedisUtil redisUtil) {
        this.iSysDeptService = iSysDeptService;
        this.redisUtil = redisUtil;
    }

    // 查询全部记录
    // <V> List<V> listObjs(Function<? super Object, V> mapper);
    // 根据 Wrapper 条件，查询全部记录
    // List<Object> listObjs(Wrapper<T> queryWrapper);
    // 根据 Wrapper 条件，查询全部记录
    // <V> List<V> listObjs(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);

//    @ApiOperation：用在请求的方法上，说明方法的用途、作用
//
//    参数	描述
//    value	说明方法的用途、作用
//    notes	方法的备注说明
//    tags	操作标签，非空时将覆盖value的值
//    response	响应类型（即返回对象）
//    responseContainer	声明包装的响应容器（返回对象类型）。有效值为 “List”, “Set” or “Map”。
//    responseReference	指定对响应类型的引用。将覆盖任何指定的response（）类
//    httpMethod	指定HTTP方法，“GET”, “HEAD”, “POST”, “PUT”, “DELETE”, “OPTIONS” and “PATCH”
//    responseHeaders	响应头列表
//    code	响应的HTTP状态代码。默认 200
//    hidden	默认为false， 配置为true 将在文档中隐藏
    @RequestMapping("listObjsByMapper")
    @ApiOperation(value = "查询所有列表信息", notes="查询所有列表信息!")
    public List<Object> listObjsByMapper() {
        String key = "SysDeptController:listObjsByMapper:";
        List<Object> obj = (List<Object>) redisUtil.get(key);
        if (obj == null) {
            obj = iSysDeptService.listObjs(x -> {
                log.info("x==>" + x);
                return x;
            });
            redisUtil.set(key, obj);
        }
        return obj;
    }


    /**
     * @return java.util.List<java.lang.Object>
     * @throws
     * @title listObjs
     * @description 查询全部记录 List<Object> listObjs();
     * @author WYL
     * @updateTime 2021/8/23 23:17
     */
    @RequestMapping("listObjs")
    public List<Object> listObjs() {
        String key = "SysDeptController:listObjs:";
        List<Object> obj = (List<Object>) redisUtil.get(key);
        if (obj == null) {
            obj = iSysDeptService.listObjs();
            redisUtil.set(key, obj);
        }
        return obj;
    }

    /**
     * @return java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     * @throws
     * @title listMapsByQueryWrapper
     * @description 查询列表 List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper);
     * @author WYL
     * @updateTime 2021/8/23 23:15
     */
    @RequestMapping("listMapsByQueryWrapper")
    public List<Map<String, Object>> listMapsByQueryWrapper() {
        String key = "SysDeptController:listMapsByQueryWrapper:";
        List<Map<String, Object>> obj = (List<Map<String, Object>>) redisUtil.get(key);
        if (obj == null) {
            QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("del_flag", 1);
            obj = iSysDeptService.listMaps(queryWrapper);
            redisUtil.set(key, obj);
        }
        return obj;
    }

    /**
     * @return java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     * @throws
     * @title listMaps
     * @description 查询所有列表 List<Map<String, Object>> listMaps();
     * @author WYL
     * @updateTime 2021/8/23 23:10
     */
    @RequestMapping("listMaps")
    public List<Map<String, Object>> listMaps() {
        String key = "SysDeptController:listMaps:";
        List<Map<String, Object>> obj = (List<Map<String, Object>>) redisUtil.get(key);
        if (obj == null) {
            obj = iSysDeptService.listMaps();
            redisUtil.set(key, obj);
        }
        return obj;
    }

    /**
     * @return java.util.List<com.yilong.springcloudalibabanacos.sys.entity.SysDept>
     * @throws
     * @title listByMap
     * @description 查询（根据 columnMap 条件） Collection<T> listByMap(Map<String, Object> columnMap);
     * @author WYL
     * @updateTime 2021/8/23 23:09
     */
    @RequestMapping("listByMap")
    public List<SysDept> listByMap() {
        String key = "SysDeptController:listByMap:";
        Map<String, Object> map = new HashMap<>(16);
        map.put("del_flag", 0);
        map.put("name", "updateByWhereWrapper");
        List<SysDept> obj = (List<SysDept>) redisUtil.get(key);
        if (obj == null) {
            obj = iSysDeptService.listByMap(map);
            redisUtil.set(key, obj);
        }
        return obj;
    }

    /**
     * @return java.util.List<com.yilong.springcloudalibabanacos.sys.entity.SysDept>
     * @throws
     * @title listByIds
     * @description 查询（根据ID 批量查询）Collection<T> listByIds(Collection<? extends Serializable> idList);
     * @author WYL
     * @updateTime 2021/8/22 21:43
     */
    @RequestMapping("listByIds")
    public List<SysDept> listByIds() {
        String key = "SysDeptController:listByIds:";
        List<Long> list = new ArrayList<>();
        LongStream.range(1500, 3000).forEach(list::add);
        List<SysDept> obj = (List<SysDept>) redisUtil.get(key);
        if (obj == null) {
            obj = iSysDeptService.listByIds(list);
            redisUtil.set(key, obj);
        }
        return obj;
    }

    /**
     * @return java.util.List<com.yilong.springcloudalibabanacos.sys.entity.SysDept>
     * @throws
     * @title listByQueryWrapper
     * @description 查询列表 List<T> list(Wrapper<T> queryWrapper);
     * @author WYL
     * @updateTime 2021/8/22 21:35
     */
    @RequestMapping("listByQueryWrapper")
    public List<SysDept> listByQueryWrapper() {
        String key = "SysDeptController:listByQueryWrapper:";
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag", 0);
        List<SysDept> obj = (List<SysDept>) redisUtil.get(key);
        if (obj == null) {
            obj = iSysDeptService.list(queryWrapper);
            redisUtil.set(key, obj);
        }
        return obj;
    }

    /**
     * @return java.util.List<com.yilong.springcloudalibabanacos.sys.entity.SysDept>
     * @throws
     * @title list
     * @description 查询所有 List<T> list();
     * @author WYL
     * @updateTime 2021/8/22 21:29
     */
    @RequestMapping("list")
    public List<SysDept> list() {
        String key = "SysDeptController:list:";
        List<SysDept> obj = (List<SysDept>) redisUtil.get(key);
        if (obj == null) {
            obj = iSysDeptService.list();
            redisUtil.set(key, obj);
        }
        return obj;
    }

    /**
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @throws
     * @title getObj
     * @description 根据 Wrapper，查询一条记录
     * <V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);
     * @author WYL
     * @updateTime 2021/8/22 11:09
     */
    @RequestMapping("getObj")
    public Object getObj() {
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag", 0);
        queryWrapper.eq("parent_id", 1519);
        String key = "SysDeptController:getObj:";
        Object obj = redisUtil.get(key);
        if (obj == null) {
            obj = iSysDeptService.getObj(queryWrapper, s -> s);
            redisUtil.set(key, obj, 100);
        }
        return obj;
    }

    /**
     * @return com.yilong.springcloudalibabanacos.sys.entity.SysDept
     * @throws
     * @title getMap
     * @description 根据 Wrapper，查询一条记录 Map<String, Object> getMap(Wrapper<T> queryWrapper);
     * @author WYL
     * @updateTime 2021/8/22 11:03
     */
    @RequestMapping("getMap")
    public Map<String, Object> getMap() {
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag", 0);
        queryWrapper.eq("parent_id", 1519);
        String key = "SysDeptController:getMap:";
        Map<String, Object> map = (Map<String, Object>) redisUtil.get(key);
        if (map == null) {
            map = iSysDeptService.getMap(queryWrapper);
            redisUtil.set(key, map, 100);
        }
        return map;
    }

    /**
     * @return com.yilong.springcloudalibabanacos.sys.entity.SysDept
     * @throws
     * @title getOneByQueryWrapper
     * @description 根据 Wrapper，查询一条记录 T getOne(Wrapper<T> queryWrapper, boolean throwEx);
     * @author WYL
     * @updateTime 2021/8/22 10:43
     */
    @RequestMapping("getOneByQueryWrapper")
    public SysDept getOneByQueryWrapper() {
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag", 0);

        String key = "SysDeptController:getOneByQueryWrapper:";
        SysDept sysDept = (SysDept) redisUtil.get(key);
        if (sysDept == null) {
            sysDept = iSysDeptService.getOne(queryWrapper, false);
            redisUtil.set(key, sysDept, 100);
        }
        return sysDept;
    }

    /**
     * @return com.yilong.springcloudalibabanacos.sys.entity.SysDept
     * @throws
     * @title getOne
     * @description T getOne(Wrapper<T> queryWrapper);
     * 根据 Wrapper，查询一条记录。结果集，
     * 如果是多个会抛出异常，随机取一条加上限制条件 wrapper.last("LIMIT 1")
     * @author WYL
     * @updateTime 2021/8/21 15:30
     */
    @RequestMapping("getOne")
    public SysDept getOne() {
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag", 0);
        queryWrapper.last("LIMIT 1");
        String key = "SysDeptController:getOne:";
        SysDept sysDept = (SysDept) redisUtil.get(key);
        if (sysDept == null) {
            sysDept = iSysDeptService.getOne(queryWrapper, false);
            redisUtil.set(key, sysDept, 100);
        }
        return sysDept;
    }

    /**
     * @return com.yilong.springcloudalibabanacos.sys.entity.SysDept
     * @throws
     * @title getById
     * @description 根据 ID 查询 T getById(Serializable id);
     * @author WYL
     * @updateTime 2021/8/21 15:28
     */
    @RequestMapping("getById")
    public SysDept getById() {
        return iSysDeptService.getById(1519);
    }

    /**
     * @return boolean
     * @throws
     * @title updateBatchByIdAndSize
     * @description 根据ID 批量更新 boolean updateBatchById(Collection<T> entityList, int batchSize);
     * @author WYL
     * @updateTime 2021/8/18 23:01
     */
    @RequestMapping("updateBatchByIdAndSize")
    public boolean updateBatchByIdAndSize() {
        List<SysDept> list = new ArrayList<>();
        LongStream.range(1500, 3000).forEach(x -> {
            SysDept sysDept = new SysDept();
            sysDept.setDeptId(x);
            sysDept.setParentId(x);
            sysDept.setName("updateBatchByIdAndSize");
            sysDept.setDelFlag(0);
            list.add(sysDept);
        });
        return iSysDeptService.updateBatchById(list, 2);
    }

    /**
     * @return boolean
     * @throws
     * @title updateBatchById
     * @description 根据ID 批量更新 boolean updateBatchById(Collection<T> entityList);
     * @author WYL
     * @updateTime 2021/8/18 22:59
     */
    @RequestMapping("updateBatchById")
    public boolean updateBatchById() {
        List<SysDept> list = new ArrayList<>();
        LongStream.range(1500, 2000).forEach(x -> {
            SysDept sysDept = new SysDept();
            sysDept.setDeptId(x);
            sysDept.setParentId(x);
            sysDept.setName("updateBatchById");
            sysDept.setDelFlag(0);
            list.add(sysDept);
        });
        return iSysDeptService.updateBatchById(list);
    }

    /**
     * @return boolean
     * @throws
     * @title updateById
     * @description 根据 ID 选择修改 boolean updateById(T entity);
     * @author WYL
     * @updateTime 2021/8/18 22:55
     */
    @RequestMapping("updateById")
    public boolean updateById() {
        SysDept sysDept = new SysDept();
        sysDept.setDeptId(1519L);
        sysDept.setParentId(0L);
        sysDept.setName("updateById");
        sysDept.setDelFlag(0);
        return iSysDeptService.updateById(sysDept);
    }

    /**
     * @return boolean
     * @throws
     * @title updateByWhereWrapper
     * @description 根据 whereWrapper 条件，更新记录 boolean update(T updateEntity, Wrapper<T> whereWrapper);
     * @author WYL
     * @updateTime 2021/8/18 22:52
     */
    @RequestMapping("updateByWhereWrapper")
    public boolean updateByWhereWrapper() {
        UpdateWrapper<SysDept> wrapper = new UpdateWrapper<>();
        SysDept sysDept = new SysDept();
        sysDept.setDeptId(1519L);
        sysDept.setParentId(0L);
        sysDept.setName("updateByWhereWrapper");
        sysDept.setDelFlag(0);
        wrapper.eq("del_flag", 0);
        return iSysDeptService.update(sysDept, wrapper);
    }

    /**
     * @return boolean
     * @throws
     * @title update
     * @description 根据 UpdateWrapper 条件，更新记录 需要设置sqlset boolean update(Wrapper<T> updateWrapper);
     * @author WYL
     * @updateTime 2021/8/18 22:49
     */
    @RequestMapping("update")
    public boolean update() {
        UpdateWrapper<SysDept> wrapper = new UpdateWrapper<>();
        wrapper.set("name", "测试update-UpdateWrapper");
        wrapper.eq("del_flag", 0);
        return iSysDeptService.update(wrapper);
    }

    /**
     * @return boolean
     * @throws
     * @title removeByIds
     * @description 删除（根据ID 批量删除）boolean removeByIds(Collection<? extends Serializable> idList);
     * @author WYL
     * @updateTime 2021/8/17 23:12
     */
    @RequestMapping("removeByIds")
    public boolean removeByIds() {
        List<Integer> list = new ArrayList<>(16);
        IntStream.range(2000, 3000).forEach(list::add);
        return iSysDeptService.removeByIds(list);
    }

    /**
     * @return boolean
     * @throws
     * @title removeByMap
     * @description 根据 columnMap 条件，删除记录  boolean removeByMap(Map<String, Object> columnMap);
     * @author WYL
     * @updateTime 2021/8/17 22:56
     */
    @RequestMapping("removeByMap")
    public boolean removeByMap() {
        Map<String, Object> columnMap = new HashMap<>(16);
        columnMap.put("name", "测试saveOrUpdateBatchBySize1518");
        columnMap.put("del_flag", 0);
        return iSysDeptService.removeByMap(columnMap);
    }

    /**
     * @return boolean
     * @throws
     * @title removeById
     * @description 根据 entity 条件，删除记录 boolean removeById(Serializable id);
     * @author WYL
     * @updateTime 2021/8/17 22:32
     */
    @RequestMapping("removeById")
    public boolean removeById() {
        return iSysDeptService.removeById(1518);
    }

    /**
     * @return boolean
     * @throws
     * @title remove
     * @description 据 entity 条件，删除记录 boolean remove(Wrapper<T> queryWrapper);
     * @author WYL
     * @updateTime 2021/8/17 22:26
     */
    @RequestMapping("remove")
    public boolean remove() {
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        Integer x = 1516;
        SysDept sysDept = new SysDept();
        sysDept.setDeptId(Long.parseLong(x + ""));
        sysDept.setParentId(Long.parseLong(x + ""));
        sysDept.setName("测试saveOrUpdateBatchBySize" + x);
        sysDept.setOrderNum(x);
        sysDept.setDelFlag(0);
        queryWrapper.setEntity(sysDept);
        return iSysDeptService.remove(queryWrapper);
    }

    /**
     * @return boolean
     * @throws
     * @title saveOrUpdateBatchBySize
     * @description 批量修改插入 boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);
     * @author WYL
     * @updateTime 2021/8/17 22:21
     */
    @RequestMapping("saveOrUpdateBatchBySize")
    public boolean saveOrUpdateBatchBySize() {
        List<SysDept> list = new ArrayList<>();
        IntStream.range(1500, 3000).forEach(x -> {
            SysDept sysDept = new SysDept();
            sysDept.setDeptId(Long.parseLong(x + ""));
            sysDept.setParentId(Long.parseLong(x + ""));
            sysDept.setName("测试saveOrUpdateBatchBySize" + x);
            sysDept.setOrderNum(x);
            sysDept.setDelFlag(0);
            list.add(sysDept);
        });
        return iSysDeptService.saveOrUpdateBatch(list, 100);
    }

    /**
     * @return boolean
     * @throws
     * @title saveOrUpdateBatch
     * @description 批量修改插入 boolean saveOrUpdateBatch(Collection<T> entityList);
     * @author WYL
     * @updateTime 2021/8/17 22:19
     */
    @RequestMapping("saveOrUpdateBatch")
    public boolean saveOrUpdateBatch() {
        List<SysDept> list = new ArrayList<>();
        IntStream.range(1500, 3000).forEach(x -> {
            SysDept sysDept = new SysDept();
            sysDept.setDeptId(Long.parseLong(x + ""));
            sysDept.setParentId(Long.parseLong(x + ""));
            sysDept.setName("测试saveOrUpdateBatch" + x);
            sysDept.setOrderNum(x);
            sysDept.setDelFlag(0);
            list.add(sysDept);
        });
        return iSysDeptService.saveOrUpdateBatch(list);
    }

    /**
     * @return boolean
     * @throws
     * @title saveOrUpdateByWrapper
     * @description 根据updateWrapper尝试更新，否继续执行saveOrUpdate(T)方法
     * boolean saveOrUpdate(T entity, Wrapper<T> updateWrapper);
     * @author WYL
     * @updateTime 2021/8/17 0:09
     */
    @RequestMapping("saveOrUpdateByWrapper")
    public boolean saveOrUpdateByWrapper() {
        SysDept sysDept = new SysDept();
        sysDept.setDeptId(4017L);
        sysDept.setParentId(0L);
        sysDept.setName("测试saveOrUpdateByWrapper4017L");
        sysDept.setOrderNum(0);
        sysDept.setDelFlag(0);
        UpdateWrapper<SysDept> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(SysDept::getDelFlag, 0);
        return iSysDeptService.saveOrUpdate(sysDept);
    }

    /**
     * @return boolean
     * @throws
     * @title saveOrUpdate
     * @description TableId 注解存在更新记录，否插入一条记录
     * boolean saveOrUpdate(T entity);
     * @author WYL
     * @updateTime 2021/8/17 0:03
     */
    @RequestMapping("saveOrUpdate")
    public boolean saveOrUpdate() {
        SysDept sysDept = new SysDept();
        sysDept.setDeptId(1516L);
        sysDept.setParentId(0L);
        sysDept.setName("测试saveOrUpdate");
        sysDept.setOrderNum(0);
        sysDept.setDelFlag(0);
        return iSysDeptService.saveOrUpdate(sysDept);
    }


    /**
     * @return java.lang.Object
     * @throws
     * @title sysDeptList
     * @description
     * @author WYL
     * @updateTime 2021/8/16 23:29
     */
    @RequestMapping()
    public Object sysDeptList() {
        return iSysDeptService.page(new Page<>(1, 10));
    }

    // 插入（批量）
    //
    //
    //

    /**
     * @return boolean
     * @throws
     * @title save
     * @description 插入一条记录（选择字段，策略插入） boolean save(T entity);
     * @author WYL
     * @updateTime 2021/8/16 23:41
     */
    @RequestMapping("save")
    public boolean save() {
        SysDept sysDept = new SysDept();
        sysDept.setDeptId(0L);
        sysDept.setParentId(0L);
        sysDept.setName("测试");
        sysDept.setOrderNum(0);
        sysDept.setDelFlag(0);
        return iSysDeptService.save(sysDept);
    }

    /**
     * @return boolean
     * @throws
     * @title saveBatch
     * @description 插入（批量）boolean saveBatch(Collection<T> entityList);
     * @author WYL
     * @updateTime 2021/8/16 23:44
     */
    @RequestMapping("saveBatch")
    public boolean saveBatch() {
        List<SysDept> list = new ArrayList<>();
        IntStream.range(1, 1500).forEach(x -> {
            SysDept sysDept = new SysDept();
            sysDept.setDeptId(Long.parseLong(x + ""));
            sysDept.setParentId(Long.parseLong(x + ""));
            sysDept.setName("测试" + x);
            sysDept.setOrderNum(x);
            sysDept.setDelFlag(0);
            list.add(sysDept);
        });
        return iSysDeptService.saveBatch(list);
    }

    /**
     * @return boolean
     * @throws
     * @title saveBatch
     * @description 插入（批量）boolean saveBatch(Collection<T> entityList, int batchSize);
     * @author WYL
     * @param: batchSize
     * @updateTime 2021/8/16 23:52
     */
    @RequestMapping("saveBatchBySize")
    public boolean saveBatch(Integer batchSize) {
        List<SysDept> list = new ArrayList<>();
        IntStream.range(1, 1000).forEach(x -> {
            SysDept sysDept = new SysDept();
            sysDept.setDeptId(Long.parseLong(x + ""));
            sysDept.setParentId(Long.parseLong(x + ""));
            sysDept.setName("测试" + x);
            sysDept.setOrderNum(x);
            sysDept.setDelFlag(0);
            list.add(sysDept);
        });
        return iSysDeptService.saveBatch(list, batchSize);
    }


}
